package org.tailor
class ClientController {
   
    def scaffold = Client
    
    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [clientInstanceList: Client.list(params), clientInstanceTotal: Client.count()]
    }
    
    def newClient = {}
    
    def profile = {
	[client: Client.get(params.id) ]
    }
        
    def clientRegister = {
        if (params) {
            def client = new Client(params)
            if (client.validate()) {
                client.save()
                flash.message = "Client account created"
                redirect(uri: '/')
            } else {
                flash.message = "Registration Error"
                return [client:client]
            }       
        }
    }

    def show = {
        def clientInstance = Client.get(params.id)
        if (!clientInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'client.label', default: 'Client'), params.id])}"
            redirect(action: "list")
        }
        else {
            [clientInstance: clientInstance]
        }
    }
    
    def findByClientId={
        if (params) {
            def client = Client.get(params.id)
            if (client.validate()) {
                [client:client]
            } else {
                println "clientId not found"
                return false
            }       
        }
    }
    
    def clientProfile = {
        if (params.id) {
            def client = Client.findByClientId(params.id)
            if (client) {
                return [ profile : client.profile, userId : client.userId ]
            } else {
                response.sendError(404)
            }
        }
    }
    
    def modifyPassword = {
		def client = Client.get(params.id)
		[ client: client ]
	}
        
    def updatePassword = {
		def client = Client.get(params.id)	
		boolean correctPassword = (params.password == client.password)	
		boolean passwordsMatch = (params.newpassword == params.confirm)
		if (correctPassword && passwordsMatch)	{		
			client.password = params.password
			client.save()
			redirect(action: 'profile', id: user.id)
		} else {
			flash.message = "Something doesn't add up, dude. Try again."
			redirect(action: 'changePassword', id: user.id)
		}
	}
        
        
        /*
    static def createAccount(String name, long UserID){
        def clientInstance = new Client()
        clientInstance.firstName = new ClientController(firstName:firstName)
        clientInstance.clientID = new ClientController(UserID:UserID)            
    }
    
    static def addMeasurement (long measurementID, long chest){
        def clientInstance = new Client()
        clientInstance.measurement(measurementID, chest)
    }
    
    
    static removeMeasurement(){
        def clientInstance = new Client()
        clientInstance.measurement(0,0)
    }
    
    static def modifyMeasurement(long newChest){
        def clientInstance = new Client()
        def U=clientInstance.measurementID()
        def collarInstance = new Collar()
        collarInstance.addMeasurement(U, newChest)
    }
    */
    
        def search = {}

        def results = {
		def clients = Client.findByClientId("%${params.clientId}%")
		return [ clients: clients, term : params.clientId ]
            }

    
        def advSearch = {}

        def advResults = {
          def profile1 = Profile.metaClass.properties*.name
          def profile2 = Profile.withCriteria {
                "${params.queryType}" {

                        params.each { field, value ->
                            
                            if (profile1.grep(field) && value) {
                                ilike(field, value)
                            }
                      }
                }
        }
        [profile2:profile2]
    }


    
    def timeLogin = {
        def client = Client.findByClientId(params.id)
        [ client : client ]
    }

    


    def create = {
        def clientInstance = new Client()
        clientInstance.properties = params
        return [clientInstance: clientInstance]
		
		
    }

    def save = {
        def clientInstance = new Client(params)
        if (clientInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'client.label', default: 'Client'), clientInstance.id])}"
            redirect(action: "show", id: clientInstance.id)
        }
        else {
            render(view: "create", model: [clientInstance: clientInstance])
        }
    }

  

    def edit = {
        def clientInstance = Client.get(params.id)
        if (!clientInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'client.label', default: 'Client'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [clientInstance: clientInstance]
        }
    }

    def update = {
        def clientInstance = Client.get(params.id)
        if (clientInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (clientInstance.version > version) {
                    
                    clientInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'client.label', default: 'Client')] as Object[], "Another user has updated this Client while you were editing")
                    render(view: "edit", model: [clientInstance: clientInstance])
                    return
                }
            }
            clientInstance.properties = params
            if (!clientInstance.hasErrors() && clientInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'client.label', default: 'Client'), clientInstance.id])}"
                redirect(action: "show", id: clientInstance.id)
            }
            else {
                render(view: "edit", model: [clientInstance: clientInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'client.label', default: 'Client'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def clientInstance = Client.get(params.id)
        if (clientInstance) {
            try {
                clientInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'client.label', default: 'Client'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'client.label', default: 'Client'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'client.label', default: 'Client'), params.id])}"
            redirect(action: "list")
        }
    }
}

